package com.qf.bank.util;/*
 * @author     : XXX
 * @date       : 2022/4/29 18:20
 * @description: some description
 */

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    static {
        try {
            Properties properties = new Properties();
            InputStream is = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
            properties.load(is);
            is.close();
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
//            System.out.println(driver+"\t"+url);
            Class.forName(driver);
        } catch (Exception e) {
            System.out.println("注册驱动失败:"+e.getMessage());
        }
    }
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            System.out.println("获取连接失败:"+e.getMessage());
        }
        return null;
    }
    public static void closeAll(ResultSet rs, Statement stmt,Connection conn){
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(getConnection());
    }
}
